import Vue from 'vue'
import router from '@/router'
import store from '@/store'
import moment from 'moment'

/**
 * 获取uuid
 */
export function getUUID () {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
    return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
  })
}

/**
 * 是否有权限
 * @param {*} key
 */
export function isAuth (key) {
  return JSON.parse(sessionStorage.getItem('permissions') || '[]').indexOf(key) !== -1 || false
}

/**
 * 树形数据转换
 * @param {*} data
 * @param {*} id
 * @param {*} pid
 */
export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
  var res = []
  var temp = {}
  for (var i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (var k = 0; k < data.length; k++) {
    if (temp[data[k][pid]] && data[k][id] !== data[k][pid]) {
      if (!temp[data[k][pid]]['children']) {
        temp[data[k][pid]]['children'] = []
      }
      if (!temp[data[k][pid]]['_level']) {
        temp[data[k][pid]]['_level'] = 1
      }
      data[k]['_level'] = temp[data[k][pid]]._level + 1
      temp[data[k][pid]]['children'].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}

/**
 * 清除登录信息
 */
export function clearLoginInfo () {
  Vue.cookie.delete('token')
  store.commit('resetStore')
  router.options.isAddDynamicMenuRoutes = false
}

/**
 * 将枚举值转换为显示值
 * @param type
 * @param key
 * @returns {*}
 */
export function convertEnumDisplayValue (type, key) {
  if (!!type) {
    let enumType = window.ENUMS[type]
    if (!!enumType) {
      let target = enumType.filter(function (item) {
        return item.key === key
      })
      if (!!target && target.length > 0) {
        return target[0].value
      } else {
        return key
      }
    } else {
      return key
    }
  } else {
    return key
  }
}

// 格式化时间
export function datetimeTableCellFormat (row, column) {
  let date = row[column.property]
  if (date === undefined || date === '') {
    return ''
  }
  return moment(date).format('YYYY-MM-DD HH:mm:ss')
}

export function dateTableCellFormat (row, column) {
  let date = row[column.property]
  if (date === undefined || date === '') {
    return ''
  }
  return moment(date).format('YYYY-MM-DD')
}

// 格式化性别
export function genderTableCellFormat (row, column) {
  let key = row[column.property]
  if (key === undefined || key === '') {
    return ''
  } else {
    return convertEnumDisplayValue('GENDER', key)
  }
}

// 布尔值格式化
export function boolValueTableCellFormat (row, column) {
  let key = row[column.property]
  if (key === undefined || key === '') {
    return ''
  } else {
    return convertEnumDisplayValue('COMMON_BOOL', key)
  }
}

// 任务处理类型 PROCESS_TYPE
export function processTypeTableCellFormat (row, column) {
  let key = row[column.property]
  if (key === undefined || key === '') {
    return ''
  } else {
    return convertEnumDisplayValue('PROCESS_TYPE', key)
  }
}

// 任务处理类型 TEL_OP_TYPE
export function operatorTableCellFormat (row, column) {
  let key = row[column.property]
  if (key === undefined || key === '') {
    return ''
  } else {
    return convertEnumDisplayValue('TEL_OP_TYPE', key)
  }
}
